package com.wmyg.antifake.controller;

import com.github.pagehelper.PageInfo;
import com.wmyg.antifake.bean.Bill;
import com.wmyg.antifake.bean.Factory;
import com.wmyg.antifake.bean.FactoryBean;
import com.wmyg.antifake.service.BillService;
import com.wmyg.antifake.service.FactoryService;
import com.wmyg.antifake.utils.ReplayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/dataStatistics")
public class webSockController {

    @Autowired
    FactoryService factoryService;

    @Autowired
    BillService billService;

    @RequestMapping(value = "index")
    public ModelAndView dataIndex(ModelAndView mv) {
        List<Factory>  factoryList = factoryService.findAll();
        mv.addObject("factoryList",factoryList);
        mv.setViewName("dataStatistics/webSocket");
        return mv;
    }

    @RequestMapping(value = "dataList")
    public ModelAndView dataListIndex(ModelAndView mv){
        List<Factory>  factoryList = factoryService.findAll();
        mv.addObject("factoryList",factoryList);
        mv.setViewName("dataStatistics/workOrderList");
        return mv;
    }

    @RequestMapping(value = "workOrderList")
    public ReplayUtils workOrderList(Bill bill) throws ParseException{
        if (!bill.getStartEndTime().contains("操作时间")) {
        String[] str = bill.getStartEndTime().split("-");
        if (str.length == 2) {
            bill.setCreatime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(str[0]+" 00:00:00"));
            bill.setEndTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(str[1]+" 24:00:00"));
        } else {
            bill.setCreatime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(str[0]+" 00:00:00"));
            bill.setEndTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(str[0]+" 24:00:00"));
        }
        }
        PageInfo<Bill> pageInfo = billService.selectBillList(bill);
        return new ReplayUtils(pageInfo.getTotal(), pageInfo.getList(),0);
    }

    @RequestMapping(value = "/factoryList")
    public ModelAndView factoryList(ModelAndView mv,FactoryBean factory){
        mv.addObject("factoryId",factory.getFactoryId());
        //mv.setViewName("dataStatistics/factoryList");
        if(factory.getFactoryId() == 0) {
            mv.setViewName("dataStatistics/factoryList");
        }else{
            mv.setViewName("dataStatistics/details");
        }
        return mv;
    }

}

